/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *  SRVBlackfinController.m - iPhoneSRV console for Surveyor SRV-1 and SVS 
 *
 *  Created by Nick Kitchener.  Copyright (C) 2009  Surveyor Corporation 
 *            and Nick Kitchener.  All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details (www.gnu.org/licenses)
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

//	This class sits on a thread stack boundary. Use the createInstance rather than alloc.
//	The class uses message passing using ports to bridge the stack boundary.
//

#include <stdio.h>

#import "SRVBlackfinController.h"
#import "SRVInterthreadMessage.h"
#import "SRVInterthreadMessageConnect.h"
#import "SRVInterthreadMessageDisconnect.h"
#import "SRVInterthreadMessageStreamable.h"
#import "SRVInterthreadMessageCheckIn.h"
#import "SRVStateUninitialised.h"

@implementation SRVBlackfinController
@synthesize connected;
@synthesize initialised;
@synthesize connectionStateListener;

//===================================================================
// Class factory for instancing

// Class factory for creating instances of SRVIOStream
+(SRVBlackfinController*)createInstance {
	SRVBlackfinController *instance = [[[SRVBlackfinController alloc] init] retain];
	return instance;
}

// called by the factory caller thread stack..
- (id)init {
	if(self = [super init]) {
		// initialise object attributes
		initialised = NO;
		connected = NO;
		
		// initialise the child before we start the child thread.
		currentState = [[SRVStateUninitialised alloc] initWithStateSwitcher:self];
		
		bfinIOStream = [ [SRVIOStream alloc] init];
		[bfinIOStream setParent:self];
		[bfinIOStream setParentThread:[NSThread currentThread]];
		
		// start child thread - note thread will initialise child runtime so we need a checkin to indicate this has been completed.
		[NSThread detachNewThreadSelector:@selector(startChildThread:)
								 toTarget:bfinIOStream withObject:@"DUMMY"];
	}
	return self;
}

- (void)finalize {
	if(bfinIOStream)
		[bfinIOStream finalize];
	[super finalize];
}

-(void)setState:(SRVState*)newState {
	NSLog(@"State change %@ to %@ START", currentState, newState);
	[newState initWithStateSwitcher:self];
	SRVState *s = currentState;
	currentState = newState;
	[currentState stateInitialise];
	[s release];
	NSLog(@"State change %@ to %@ END", currentState, newState);
}

//-------------------------------------------------------------------
// Inter-thread communications
// Between calling thread stack and the child thread stack running SRVIOStream

// Send message to SRVIOStream thread.
-(void)sendMessage:(SRVInterthreadMessage*)message {
	[bfinIOStream performSelector:@selector(handleParentMessage:) onThread:[bfinIOStream thread] withObject:message waitUntilDone:NO];
}

// Receive message from SRVIOStream child thread.
- (void)handleChildMessage:(SRVInterthreadMessage *)msg {
	[currentState handleChildMessage:msg];
}


// operations

- (void) connectToURL:(NSString*)ipAddress port:(NSUInteger)port {
	NSLog(@"  %@ Connect to URL", currentState);
	[currentState connectToURL:ipAddress port:port];
}

- (void) disconnect {
	[currentState disconnect];
}

- (void) setOperation:(SRVOperation *)operation {
	[currentState setOperation:operation];
}

@end



